package com.itheima_test;

import java.io.*;
import java.util.ArrayList;

public class Test2 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //写出对象
        writeStudent();
        //读取对象
        readStudent();
    }

    private static void readStudent() throws IOException, ClassNotFoundException {
        //读取对象
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("myOtherStream\\object.txt"));
        //读取一次
        ArrayList<Student> list =(ArrayList<Student>) ois.readObject();
        //释放资源
        ois.close();
        //遍历集合
        for (Student s : list){
            System.out.println(s);
        }

    }

    private static void writeStudent() throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("myOtherStream\\object.txt"));

        Student s1 = new Student("张三",23);
        Student s2 = new Student("李四",24);
        Student s3 = new Student("王五",25);

        ArrayList<Student> list = new ArrayList<Student>();
        list.add(s1);
        list.add(s2);
        list.add(s3);

        //写出集合对象
        oos.writeObject(list);
        oos.close();
    }
}
